﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ini;


namespace CHtmlLog
{
    class CLog
    {
        private long m_iRunNum = 0;
        private long m_iInc = 0;

//        private RichTextBox m_rtbErrors = null;
        private IniFile m_oIni;
        private DateTime dStart;
        private DateTime dEnd;
        private DateTime dStepStart;
        private DateTime dStepEnd;
        private string m_sFolder;

        private string m_sStepHtml = @"
<Table class=""gridtable"" style=""border-width: 1px; border-style: solid; padding: 1px; margin: 1px; background-color: #FFFFFF; width: 100%;"">
<TR><th>Step Id</th><th>Step Name</th><th>Start Time</th><th>End Time</th><th>Inputs</th><th>Outputs</th><th>Input XML</th><th>Output XML</th></TR>
%Step%
</Table>
        ";
        private string m_sHtml = @"
<html xmlns=""http://www.w3.org/1999/xhtml"" style=""font-family: 'Courier New', Courier, monospace; font-weight: normal; background-color: #C0C0C0;"">
<head>
    <title>Sequence %SequenceName% - Log Run Number %RunNumber%</title>
    <META http-equiv=""Content-Style-Type"" content=""text/css"">
   <link href=""default.css"" rel=""stylesheet"" type=""text/css"" />
</head>
<body>
<br/><h1>%SequenceName% - Run Number %RunNumber% (Time=%Time%ms)</h1><br/> 
%Step%
</body>
";

        public CLog()
        {
        }

        public CLog(IniFile oIni)
        {
            m_rtbErrors = rtbErrors;
            m_oIni = oIni;

            dStart = DateTime.Now;
            m_iRunNum = Convert.ToInt64(m_oIni.IniReadValue("Settings", "RunNum", "0")) + 1;
            m_oIni.IniWriteValue("Settings", "RunNum", m_iRunNum.ToString());
            WriteMessage("Started (RunNum:" + m_iRunNum.ToString() + ") at " + DateTime.Now.ToString());
            m_iInc = 0;
            m_sFolder = m_oIni.IniReadValue("Settings", "LogFolder", "C:\\Sapiens\\Sequence\\Logs\\");

            if (!Directory.Exists(m_sFolder))
                Directory.CreateDirectory(m_sFolder);
        }

        public void StartStep(CStep oSt)
        {
            WriteMessage("    Running step '" + oSt.sName + "'");
            dStepStart = DateTime.Now;
            m_iInc++;
        }

        public void EndStep(CStep oSt)
        {
            dStepEnd = DateTime.Now;
            string sRes = "";
            string sIn = "";

            foreach (CInput oIn in oSt.lInputs)
            {
                sIn += oIn.sName + "-" + oIn.sCalValue + "<br/>";
            }


            foreach (COutput oOut in oSt.lOutputs)
            {
                WriteMessage("      Result " + oOut.sName + "=" + oOut.sCalValue);
                sRes += oOut.sName + "-" + oOut.sCalValue + "<br/>";
            }

            if (oSt.sInFilename != "")
            {
                m_sStepHtml = m_sStepHtml.Replace("%Step%", "<TR><TD>" + oSt.sId + "</TD><TD>"
                                         + oSt.sName + "</TD><TD>"
                                         + dStepStart.ToString("yyyy/MM/dd HH:mm:ss") + "</TD><TD>"
                                         + dStepEnd.ToString("yyyy/MM/dd HH:mm:ss") + "</TD><TD>"
                                         + sIn + "</TD><TD>"
                                         + sRes + "</TD><TD>"
                                         + "<a href=\"" + oSt.sInFilename + "\">Input.xml</a></TD><TD>"
                                         + "<a href=\"" + oSt.sOutFilename + "\">Output.xml</a></TD></TR>\r\n%Step%")
                                         ;
            }
            else
            {
                m_sStepHtml = m_sStepHtml.Replace("%Step%", "<TR><TD>" + oSt.sId + "</TD><TD>"
                                         + oSt.sName + "</TD><TD>"
                                         + dStepStart.ToString("yyyy/MM/dd HH:mm:ss") + "</TD><TD>"
                                         + dStepEnd.ToString("yyyy/MM/dd HH:mm:ss") + "</TD><TD>"
                                         + sIn + "</TD><TD>"
                                         + sRes + "</TD><TD>"
                                         + "-</TD><TD>"
                                         + "-</TD></TR>\r\n%Step%")
                                         ;
            }

        }

        public void WriteMessage(string sMess)
        {
            m_rtbErrors.Text = sMess + "\r\n" + m_rtbErrors.Text;
        }

        public string OutFileName()
        {
            return m_sFolder + "Out-" + m_iRunNum.ToString() + "." + m_iInc.ToString() + ".xml";
        }

        public string InFileName()
        {
            return m_sFolder + "In-" + m_iRunNum.ToString() + "." + m_iInc.ToString() + ".xml";

        }

        public void Finish(FileSequence oFileSequence)
        {
            WriteMessage("Finished at " + DateTime.Now.ToString());
            dEnd = DateTime.Now;
            WriteMessage(oFileSequence.sLastError);
            WriteMessage(oFileSequence.oSteps[oFileSequence.oSteps.Count() - 1].GetResults());
            WriteMessage("Time " + dEnd.Subtract(dStart).TotalMilliseconds.ToString() + " ms");

            m_sHtml = m_sHtml.Replace("%SequenceName%", oFileSequence.sName);
            m_sHtml = m_sHtml.Replace("%RunNumber%", m_iRunNum.ToString());
            m_sHtml = m_sHtml.Replace("%Time%", dEnd.Subtract(dStart).TotalMilliseconds.ToString());
            m_sStepHtml = m_sStepHtml.Replace("%Step%", "");
            m_sHtml = m_sHtml.Replace("%Step%", m_sStepHtml);


            using (System.IO.StreamWriter file = new System.IO.StreamWriter(m_sFolder + "Run-" + m_iRunNum.ToString() + ".html", false))
                file.Write(m_sHtml);
        }
    }
}
